//
//  AYMHomeController.m
//  QuickCure
//
//   Mr.LV
//

#import "AYMHomeController.h"
#import "AYAreaTableViewController.h"
#import "AYMHomeBottomView.h"
#import "AYMHomeBottomAboveView.h"
#import "AYMWeatherView.h"
#import "AYMFamousDocView.h"
#import "AYMCircleScrollView.h"
#import "AYMTumorViewController.h"
#import "AYMCommonGoodViewController.h"
#import "AYMWeatherModel.h"
#import "AYMLoadWatherMessage.h"
#import "AYMBaseWebViewController.h"


@interface AYMHomeController ()<AYMWeatherViewDelegate>

@property (nonatomic,weak)UIView *circleScrollView;


@end

@implementation AYMHomeController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"马医一生";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didbutton:) name:@"didButton" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didLastbutton:) name:@"didLastButton" object:nil];
    
    //更换地区通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAreaLabel:) name:@"areaNotification" object:nil];
    
    //刷新天气的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWeather) name:@"weatherNotification" object:nil];
    
    //如果模型等于空
    if (!self.weatherModel) {
        self.weatherModel = [[AYMWeatherModel alloc] init];
        //获取天气信息
        [AYMLoadWatherMessage loadWeatherWithCity:@"上海" andViewController:self];
        
    } else {

        [AYMLoadWatherMessage loadWeatherWithCity:self.weatherView.areaLabel.text andViewController:self];
    }
    
}

//通知的监听方法

-(void)didbutton:(NSNotification *)noti{
    
    BOOL isLogin = [UserDefault boolForKey:@"isUserLogiKey"];
        
    if (!isLogin) {
        
        SCLAlertView *alert = [[SCLAlertView alloc] init];
        
        [alert showError:self title:@"提示" subTitle:@"尚未登录,请登录" closeButtonTitle:@"确定" duration:0.0f];
    }else{
    
        Log(@"按钮点击了");

        UIStoryboard *board = [UIStoryboard storyboardWithName:@"Tumor" bundle:nil];
        
        AYMTumorViewController *Vc = [board instantiateViewControllerWithIdentifier:@"board"];
        Vc.buttonString = noti.userInfo[@"tet"];
        Log(@"%@",noti);
  
        [self.navigationController pushViewController:Vc animated:YES];
        
    }
}


-(void)didLastbutton:(UIButton *)button{
    
    BOOL isLogin = [UserDefault boolForKey:@"isUserLogiKey"];
    if (!isLogin) {
        
        SCLAlertView *alert = [[SCLAlertView alloc] init];
        
        [alert showError:self title:@"错误" subTitle:@"请登录" closeButtonTitle:@"确定" duration:0.0f];
    }else{
        
        AYMCommonGoodViewController *commonVc = [[AYMCommonGoodViewController alloc] init];
        [self.navigationController pushViewController:commonVc animated:YES];
        
    }
    
}

#pragma mark - 刷新天气
- (void)reloadWeather {

    [AYMLoadWatherMessage loadWeatherWithCity:self.weatherView.areaLabel.text andViewController:self];
    
}

#pragma mark - 天气的代理方法
- (void)addAreaViewController {
    
    AYAreaTableViewController *areaTabviewVc = [[AYAreaTableViewController alloc] init];
    
    [self.navigationController pushViewController:areaTabviewVc animated:YES];

}


#pragma mark - 设置天气label的值
- (void)changeAreaLabel: (NSNotification *)info{
    
    self.weatherView.areaLabel.text = info.userInfo[@"areaKey"];
    
}


//通知使用完移除

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

/**
 *  布局界面
 */
-(void)setupUI{

    AYMHomeBottomView *bottomView = [[AYMHomeBottomView alloc]init];
    [self.view addSubview:bottomView];
    
    AYMWeatherView *weatherView = [[AYMWeatherView alloc]init];
    
    self.weatherView = weatherView;
    //设置代理
    weatherView.areaDelegate = self;
    
    [self.view addSubview:weatherView];
    
    AYMFamousDocView *famousDocView = [[AYMFamousDocView alloc]init];
    [self.view addSubview:famousDocView];
    
    UIView *circleScrollView = [[UIView alloc]init];
    [self.view addSubview:circleScrollView];
    self.circleScrollView = circleScrollView;
    
    
    [weatherView makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.leading.trailing.equalTo(self.view);
        make.height.equalTo(self.view.bounds.size.height *0.1);
    }];
    
    [bottomView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.view);
        make.height.equalTo(self.view.bounds.size.height *0.45);
    }];
    
    [famousDocView makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(bottomView.top);
        make.leading.trailing.equalTo(self.view);
        make.height.equalTo(self.view.bounds.size.height *0.06);
    }];
    
    [circleScrollView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(weatherView.bottom);
        make.bottom.equalTo(famousDocView.top);
    }];

}



- (void)viewDidAppear:(BOOL)animated{
    [super viewDidDisappear:animated];
    //自动轮播器
    AYMCircleScrollView *circleScrollContentView = [[AYMCircleScrollView alloc]initWithFrame:CGRectMake(0, 0, screenW, self.circleScrollView.frame.size.height) animationDuration:3];
 
    
    //点击图片的时候要执行的代码
    circleScrollContentView.TapActionBlock = ^(NSInteger pageIndex){
        //弹出百度
        AYMBaseWebViewController *vc = [[AYMBaseWebViewController alloc]initWithUrlString:@"https://www.baidu.com" andTitle:@""];
        [self.navigationController pushViewController:vc animated:YES];
    };
    
    [self.circleScrollView addSubview:circleScrollContentView];
    
}


@end
